const express = require('express');
const router = express.Router();
const Blog = require('./../../mongoModuels/stateg/blog');
const time  = require('./../../utils/time');
const random = require('./../../utils/random');

router
.get('/getMessage', (req, res) => {
    if(req.query.pageNum && req.query.pageSize) {
        let pageNum = parseInt(req.query.pageNum);
        let pageSize = parseInt(req.query.pageSize);
        let query = Blog.blogMessage.find({'type': 1});
        query.select('-mailbox');
        Blog.blogMessage.count({'type': 1}, (err, count) => {
            query.sort({'creationTime': -1});
            query.skip((pageNum - 1) * pageSize).limit(pageSize);
            query.exec({}, (err, data) => {
                if(err) {
                    res.json({
                        status: 0,
                        message: err
                    })
                } else {
                    data.forEach(items => {
                        if(items.children.length > 0) {
                            items.children.forEach((item, i, e) => {
                                if(item.type === 2) {
                                    e.splice(i, 1);
                                }
                            })
                        }
                    })
                    res.json({
                        status: 1,
                        message: '操作成功',
                        data: {
                            data,
                            count
                        }
                    })
                }
            })
        })
    } else {
        res.json({
            status: 0,
            message: '参数不足'
        })
    }
})

.post('/setMessage', (req, res) => {
    let data;
    let creationTime = time(new Date().getTime());
    if(req.body.name && req.body.info) {
        data = {
            name: req.body.name,
            mailbox: req.body.mailbox,
            info: req.body.info,
            children: [],
            type: 1,
            creationTime: creationTime,
        }
        if(req.body.id) {
            data._id = random();
            Blog.blogMessage.update({'_id':  req.body.id}, {$push: {'children': data}}, err => {
                if(err) {
                    res.json({
                        status: 0,
                        message: err
                    })
                } else {
                    res.json({
                        status: 1,
                        message: '操作成功'
                    })
                }
            })
        } else {
            Blog.blogMessage.create(data, err => {
                if(err) {
                    res.json({
                        status: 0,
                        message: err
                    })
                } else {
                    res.json({
                        status: 1,
                        message: '操作成功'
                    })
                }
            })
        }
    } else {
        res.json({
            status: 0,
            message: '参数不足'
        })
    }
})


module.exports = router;